import 'dart:math';
/// @author: slc
/// 颜色工具类，提供多种颜色相关的实用方法
class SlcColorUtil {
  /// 预定义的 RGB 颜色数组
  static const List<int> COLOR_ARRAY_RGB = [
    0xFFFF0000,
    0xFFFF00FF,
    0xFF0000FF,
    0xFF00FFFF,
    0xFF00FF00,
    0xFFFFFF00
  ];

  /// 预定义的 Material Design 颜色数组
  static const List<int> COLOR_ARRAY_MD = [
    0xfff44336,
    0xffE91E63,
    0xff9C27B0,
    0xff673AB7,
    0xff3F51B5,
    0xff2196F3,
    0xff03A9F4,
    0xff00BCD4,
    0xff009688,
    0xff4CAF50,
    0xff8BC34A,
    0xffCDDC39,
    0xffFFEB3B,
    0xffFFC107,
    0xffFF9800,
    0xffFF5722
  ];

  /// 预定义的扩展 Material Design 颜色数组
  static const List<int> COLOR_ARRAY_MD_EXPAND = [
    0xfff44336,
    0xffE91E63,
    0xff9C27B0,
    0xff673AB7,
    0xff3F51B5,
    0xff2196F3,
    0xff03A9F4,
    0xff00BCD4,
    0xff009688,
    0xff4CAF50,
    0xff8BC34A,
    0xffCDDC39,
    0xffFFEB3B,
    0xffFFC107,
    0xffFF9800,
    0xffFF5722,
    0xff795548,
    0xff607D8B
  ];

  /// 根据平均算法获取指定数量的颜色列表
  ///
  /// [count] 要获取的颜色数量
  /// [colorArray] 可选的颜色数组，默认为 COLOR_ARRAY_RGB
  /// [safety] 安全模式标志，默认为 true
  ///
  /// @return 包含指定数量颜色的列表
  /// @throws ArgumentError 如果 colorArray 长度为 1 或者在非安全模式下 count 小于等于 2
  static List<int> getColorByAverage(int count,{List<int>? colorArray,bool safety = true}) {
    colorArray ??= COLOR_ARRAY_RGB;

    if (colorArray.length == 1) {
      throw ArgumentError("colorArray must have more than 1 color");
    }

    if (safety) {
      switch (count) {
        case 0:
          return [];
        case 1:
          return [colorArray[0]];
        case 2:
          return [colorArray[0], colorArray[1]];
      }
    } else {
      if (count <= 2) {
        throw ArgumentError("count must be greater than 2");
      }
    }

    if (colorArray.length == count) {
      return List<int>.from(colorArray);
    }

    List<int> colorList = [];
    final colorArrayLength = colorArray.length;

    for (int i = 0; i < colorArrayLength - 1; i++) {
      final startColor = colorArray[i];
      final endColor = colorArray[i + 1];

      if (i == 0) {
        colorList.add(_evaluateColor(0.0, startColor, endColor));
      }

      for (int j = 0; j < count; j++) {
        final temp = j / (count - 1);
        if (j > 0) {
          colorList.add(_evaluateColor(temp, startColor, endColor));
        }
      }
    }

    final resultColor = List<int>.filled(count, 0);
    final colorListSize = colorList.length;
    final step = colorArrayLength - 1;

    for (int i = 0, j = 0; i < colorListSize; i++) {
      if (i % step == 0) {
        resultColor[j++] = colorList[i];
        if (j >= count) break;
      }
    }

    return resultColor;
  }

  /// 根据给定的分数在两个颜色之间进行插值计算
  ///
  /// [fraction] 插值分数，范围从 0.0 到 1.0
  /// [startValue] 起始颜色值
  /// [endValue] 结束颜色值
  ///
  /// @return 插值后的颜色值
  static int _evaluateColor(double fraction, int startValue, int endValue) {
    final startA = ((startValue >> 24) & 0xFF) / 255.0;
    final startR = ((startValue >> 16) & 0xFF) / 255.0;
    final startG = ((startValue >> 8) & 0xFF) / 255.0;
    final startB = (startValue & 0xFF) / 255.0;

    final endA = ((endValue >> 24) & 0xFF) / 255.0;
    final endR = ((endValue >> 16) & 0xFF) / 255.0;
    final endG = ((endValue >> 8) & 0xFF) / 255.0;
    final endB = (endValue & 0xFF) / 255.0;

    // Convert from sRGB to linear
    double toLinear(double channel) => pow(channel, 2.2).toDouble();
    final sR = toLinear(startR);
    final sG = toLinear(startG);
    final sB = toLinear(startB);
    final eR = toLinear(endR);
    final eG = toLinear(endG);
    final eB = toLinear(endB);

    // Interpolate
    final a = startA + fraction * (endA - startA);
    final r = sR + fraction * (eR - sR);
    final g = sG + fraction * (eG - sG);
    final b = sB + fraction * (eB - sB);

    // Convert back to sRGB
    double toSRGB(double channel) => pow(channel, 1 / 2.2).toDouble();
    final srgbR = toSRGB(r) * 255;
    final srgbG = toSRGB(g) * 255;
    final srgbB = toSRGB(b) * 255;

    return (a * 255).round() << 24 |
        srgbR.round() << 16 |
        srgbG.round() << 8 |
        srgbB.round();
  }

  /// 将整数颜色值转换为十六进制字符串表示
  ///
  /// [colorInt] 整数颜色值
  ///
  /// @return 十六进制字符串表示的颜色，格式为 '#RRGGBB'
  static String int2Hex(int colorInt) {
    return '#${(colorInt & 0xFFFFFF).toRadixString(16).padLeft(6, '0').toUpperCase()}';
  }
}
